package com.biblio.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.biblio.dao.AccountDao;
import com.biblio.dom.Account;
import com.biblio.exceptions.AccountAlreadyExistsException;
import com.biblio.service.AccountService;



@Controller
@RequestMapping("/account")
public class AccountController{
	@Autowired AccountDao accountDao;
	@Autowired AccountService accountService;	
	
    @RequestMapping("/create")
    public String createAccount( ){
        return "account/form"; // JSP (this controller is not doing anything useful but giving access to the JSP).
    } 
    @RequestMapping(value={"/submit"}) 
    public ModelAndView addAccount(@ModelAttribute Account account){  

    	try {
			account = accountService.registerAccount(account, account.getUserName(),
					account.getPwd(), account.getEmail());
			accountDao.persist(account);
		} catch (AccountAlreadyExistsException e) {
			e.printStackTrace();
		}
        return new ModelAndView("redirect:/account/confirm");
	}    
    @RequestMapping("/confirm")
    public String confirmAccount(){
        return "account/confirm"; // JSP (this controller is not doing anything useful but giving access to the JSP).
    }        
}
